/**
 * http://localhost:8083/proj
 * 		取得项目根路径
 */
function getRootPath() {
	// 获取当前网址，如： http://localhost:8083/proj/meun.jsp
	var curWwwPath = window.document.location.href;
	// 获取主机地址之后的目录，如： proj/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	// 获取主机地址，如： http://localhost:8083
	var localhostPath = curWwwPath.substring(0, pos);
	// 获取带"/"的项目名，如：/proj
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (localhostPath + projectName);
}

/**
 * 查询商品分类列表数据
 */
$(function(){
	var currentPage = 1;
	ptlist(currentPage);
});

function ptlist(currentPage){
	$.ajax({
		type : 'post',
		url : getRootPath() + "/creditsR/crlist.do",
		data : {
			"currentPage" : currentPage,
			"memberId" : 'member01',
			"creditsActionId":""	
		},
		async : 'true',
		dataType : 'json',
		success : function(data){
			var rec = data.page;
			// alert(rec.list[1].productTypeName);
			var html = "";	
			for(i = 0; i < rec.list.length; i++){
				html += "<tr>";
				html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="+rec.list[i].id+" /></td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].id+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsRuleId+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsTypeId+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsRuleContent+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsActionId+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].maxValue+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].minValue+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsVallue+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].isRepeat+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].creditsUpperValue+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].startCreditsDt+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].endCreditsDt+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].remarks+"</td>";
				html += "<td style='padding-top: 18px;'>"+rec.list[i].status+"</td>";
				html += "</tr>";
			}
			$("#credists_list_1").html(html);
			
			//鼠标经过td的时候就显示td内容
		    jQuery(function(){
		        //使用class选择器;例如:list对象->tbody->td对象.
		           $("#credists_list_1 td").each(function(i){
		                       //给td设置title属性,并且设置td的完整值.给title属性.
		                       $(this).attr("title",$(this).text());

		             });
		       });
		    var pager = data.page;
			$(".pag_in").html(
					G.getPager(pager.totalRecords,
							pager.currentPage, 5, 5,
							'ptlist'));
		}
	});
}
/**
 * 模糊查找
 */
function search(currentPage) {
	var keyWord = $("#searchForm :input[id=keyword]").val();
	var starttime = $("#searchForm :input[id=starttime]").val();
	var endtime = $("#searchForm :input[id=endtime]").val();
	var status = $("#s_status").find("option:selected").val();

	$.ajax({
				type : 'post',
				url : getRootPath() + "/creditsR/crlist.do",
				data : {
					"currentPage" : currentPage,
					"memberId" : 'member01',
					"creditsActionId" : "CB00001",
					"keyWord" : keyWord,
					"starttime" : starttime,
					"endtime" : endtime
				},
				async : 'true',
				dataType : 'json',
				success : function(data) {
					var rec = data.page;
					// alert(rec.list[1].productTypeName);
					var html = "";
					for (i = 0; i < rec.list.length; i++) {
						html += "<tr>";
						html += "<td style='padding-top: 18px;'><input type='checkbox' id='ck-all' name='ck-all' value="
								+ rec.list[i].id + " /></td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].id + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsRuleId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsTypeId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsRuleContent + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsActionId + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].maxValue + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].minValue + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsVallue + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].isRepeat + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].creditsUpperValue + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].startCreditsDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].endCreditsDt + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].remarks + "</td>";
						html += "<td style='padding-top: 18px;'>"
								+ rec.list[i].status + "</td>";
						html += "</tr>";
					}
					$("#credists_list_1").html(html);

					// 鼠标经过td的时候就显示td内容
					jQuery(function() {
						// 使用class选择器;例如:list对象->tbody->td对象.
						$("#credists_list_1 td").each(function(i) {
							// 给td设置title属性,并且设置td的完整值.给title属性.
							$(this).attr("title", $(this).text());

						});
					});
					var pager = data.page;
					$(".pag_in").html(
							G.getPager(pager.totalRecords, pager.currentPage,
									10, 10, 'ptlist'));
				}
			});
}



function getCR(){
		var chk_value = [];
		var len = $('#credists_list_1 :input[name="ck-all"]').length;
		var id;
		var num = 0;
		$('input[name="ck-all"]:checked').each(function() {
			num ++;
			id = $(this).val();
		});
		if(num > 1){
			alert("每次只能选择一条记录修改@@@");
		} else {
			getCR_record(id);
		}
}

function getCR_record(id){
	$("#creditsTypeId").html("");
	$("#creditsActionId").html("");
	$.ajax({
		type : 'post',
		url : getRootPath() + "/creditsR/getCR.do",
		data : {
			"id" : id
		},
		/*async : 'true',*/
		dataType : 'json',
		success : function(data){
			rec = eval(data.record);
			ctypes = eval(data.credits_types);
			cbehavior = eval(data.credits_behavior);
			$("#creditsform :input[name=creditsRuleId]").val(rec.creditsRuleId);
			$("#creditsform :input[name=creditsRuleContent]").val(rec.creditsRuleContent);
			$("#creditsform :input[name=remarks]").val(rec.remarks);
			$("#creditsform :input[name=creditsVallue]").val(rec.creditsVallue);
			$("#creditsform :input[name=creditsUpperValue]").val(rec.creditsUpperValue);
			$("#creditsform :input[name=startCreditsDt]").val(rec.startCreditsDt);
			$("#creditsform :input[name=endCreditsDt]").val(rec.endCreditsDt);
			$("#creditsform :input[name=maxValue]").val(rec.maxValue);
			$("#creditsform :input[name=minValue]").val(rec.minValue);
			$("#creditsform :input[name=id]").val(rec.id);
			// $("#userLevelId option[value='"+rec.userLevelId+"']").attr("select","selected");
			var userl = rec.creditsTypeId;
			var userls = document.getElementById("creditsTypeId"); 
			 var ops = userls.options;
			 for(var i=0;i<ops.length; i++){
				 var userlValue = ops[i].value; 
				 if(userlValue == userl)   //这里是你要选的值  
	                {  
					 $("#creditsTypeId").val(userl).trigger("change"); 
	                }  
			 }
			 $.each(ctypes,function(){
				 $("<option/>").val(this.credits_type_id).
				 attr("selected",rec.creditsTypeId==this.credits_type_id)
				 .html(this.credits_type_name).appendTo("#creditsTypeId");
			 });
			 $.each(cbehavior,function(){
				 $("<option/>").val(this.behavior_id).
				 attr("selected",rec.creditsActionId==this.behavior_id)
				 .html(this.behavior_name).appendTo("#creditsActionId");
			 });
			$("#creditsform :radio[name=status]").val(rec.status);
			$("#creditsform :radio[name=isRepeat]").val(rec.isRepeat);
		}
	});
}

function insert(){
	$.ajax({
		type : 'post',
		url : getRootPath() + "/creditsR/crinsert.do",
		data : $('#creditsform').serialize(),
		async : 'false',
		dataType : 'json',
		success : function(data){
			if (data.resultMsg != 1) {
				layer.msg('失败',{icon: 2});
			} else{
				layer.msg('成功',{icon:1});
				$("#myModal_1").modal("hide");
				ptlist(1);
			}
		}
	});
}

function delCR(){
	var chk_value = [];
	var len = $('#credists_list_1 :input[name="ck-all"]').length;
	var ids;
	$('input[name="ck-all"]:checked').each(function() {
		ids += $(this).val()+",";
	});
	delCR_record(ids);
}

function delCR_record(ids){
	layer.confirm('是否确认删除该产品？', {
		icon:3,
	    btn: ['清算', '取消'],
	    yes: function(index, layero){
			$.ajax({
				type : 'post',
				url : getRootPath() + "/creditsR/delCR.do",
				data:{
					"ids":ids
				},
				dataType : 'json',
				success : function(data){
					if (data.resultMsg != 1) {
						layer.msg('失败',{icon: 2});
					} else{
						layer.msg('成功',{icon:1});
						ptlist(1);
					}
				}
			});
	    }, 
		no:function(index){
		    layer.close(index);
		}
	});
}

function openInsert(){
	$("#creditsform :input").val("");
	$("#creditsTypeId").html("");
	$("#creditsActionId").html("");
	$.ajax({
		type : 'post',
		url : getRootPath() + "/creditsR/openWindows.do",
		/*async : 'true',*/
		dataType : 'json',
		success : function(data){
			ctypes = eval(data.credits_types);
			cbehavior = eval(data.credits_behavior);
			$.each(ctypes,function(){
				 $("<option/>").val(this.credits_type_id)
				 .html(this.credits_type_name).appendTo("#creditsTypeId");
			 });
			$.each(cbehavior,function(){
				 $("<option/>").val(this.behavior_id)
				 .html(this.behavior_name).appendTo("#creditsActionId");
			 });
		}
	});
}